<%--
  Created by IntelliJ IDEA.
  User: Albert Lee
  Date: 2020/5/28
  Time: 18:51
  To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*" %>
<html>
<head>
    <title>数据更新</title>
</head>


<body>

<%--判断是否登录--%>
<%
    String userid=(String)session.getAttribute("userid");

    if (userid!=null) {
%> 

 <h3>UserID：<%=(String)session.getAttribute("userid")%>, 在线中..</h3>
<hr>

<%!
    String DBDRIVER="com.mysql.cj.jdbc.Driver";
    String DBURL="jdbc:mysql://localhost:3306/talknewsdatabase";
    String DBUSER="root";
    String DBPASS="123456";

%>
<%
    Connection conn=null;
    PreparedStatement pst=null;
    int rs=0;
    String id=null;
    String name=null;
    String title=null;
    String comment=null;

%>

<%
    try{
        Class.forName(DBDRIVER);
        conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
        request.setCharacterEncoding("utf-8");
        id=request.getParameter("id");
        name=request.getParameter("name");
        title=request.getParameter("title");
        comment=request.getParameter("comment");
        String sql_update="update info set name= '"+name+"' ,title= '"+title+"' ,comment= '"+comment+"' where user_id= '" +userid + "' and id= '"+id+ "'";
        pst=conn.prepareStatement(sql_update);
        rs=pst.executeUpdate();
        if(rs!=0){
            out.print("<script>alert('修改成功！');" +
            "window.location='http://localhost:8080/select_for_id.jsp?id=" +id+ " ;'</script>");
        }else{
            out.print("<script>alert('修改失败！');" +
             "window.location='http://localhost:8080/showInfo.jsp';</script>");
        }
    }
    catch(Exception e){
        out.println(e);
    }
%>


<%
    } else {
    out.print("<script>alert('未成功登录！');" +
    "window.location='http://localhost:8080/index.html;'</script>");
    }
%>

</body>
</html>



